package _1_PIO.excel.样式;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 合并单元格
 */
public class _4_MergingCells {

    @Test
    public void _2003(){}

    @Test
    public void _2007() throws IOException {

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("my sheet");

        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);
        cell.setCellValue("This is a test of merging");

        sheet.addMergedRegion(new CellRangeAddress(
                1,
                1,
                1,
                3
        ));

        sheet.addMergedRegion(new CellRangeAddress(
                3,
                5,
                1,
                3
        ));

        try(OutputStream outputStream = new FileOutputStream("D:\\workbook.xlsx")){
            workbook.write(outputStream);
        }
        finally{
            workbook.close();
        }
    }
}
